//
//  TJUserModel.m
//  BaoDian
//
//  Created by mac on 2024/8/6.
//

#import "TJUserModel.h"

@implementation TJUserModel

MJLogAllIvars


+ (NSArray *)bg_uniqueKeys {
    return @[@"account"];
}

- (NSString *)bg_tableName {//因为之前使用的是email作为唯一主键, 现在换个表名
    return [NSString stringWithFormat:@"%@", NSStringFromClass(self.class)];
}

+ (NSString *)bg_tableName {
    return [NSString stringWithFormat:@"%@", NSStringFromClass(self.class)];
}

+ (TJUserModel *)modelWithUserInfo:(NSDictionary *)info {
    TJUserModel *model = [[TJUserModel alloc] init];
    model.account = info[@"mobile"];
    model.province = info[@"province"];
    model.regType = [info[@"regType"] intValue];
    model.status = [info[@"status"] intValue];
    model.nickname = info[@"nickname"];
    model.userLevelTxt = info[@"userLevelTxt"];
    model.realName = info[@"realName"];
    model.userType = [info[@"userType"] intValue];
    model.poi = info[@"poi"];
    model.userLevel = [info[@"userLevel"] intValue];
    model.Id = info[@"id"];
    model.avatar = info[@"pic"];
    model.createDate = info[@"createDate"];
    model.expireDate = info[@"expireTime"];
    model.openId = info[@"openId"];
    model.supplierName = info[@"supplierName"];
    model.isClip = [info[@"isClip"] boolValue];
    model.poiSourceInfo = info[@"poiSourceInfo"];
    
    if ([info[@"groupId"] isKindOfClass:NSString.class]) {
        model.groupId = info[@"groupId"];
    }
    return model;
}

+ (TJUserModel *)modelWithAccount:(NSString *)account {
    TJUserModel *userModel = [TJUserModel bg_find:[TJUserModel bg_tableName] where:[NSString stringWithFormat:@"where %@=%@", bg_sqlKey(@"account"), bg_sqlValue(account)]].firstObject;
    if (!userModel) {
        userModel = [[TJUserModel alloc] init];
        userModel.account = account;
        [userModel bg_saveOrUpdateAsync:nil];
    }
    return userModel;
}

+ (TJUserModel *)currentAccountModel {
    return [self modelWithAccount:TJUserInfo.account];
}

@end
